<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="okhttp3.*" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%--流程退回调用QMS接口--%>
<%
    JSONObject jsonData = new JSONObject();
    BaseBean baseBean = new BaseBean();
    try {
        String requestid = Util.null2String(request.getParameter("requestid"));
        baseBean.writeLog(">>>>>receiptOaOption.jsp>>>>>requestid="+requestid);
        String type = Util.null2String(request.getParameter("type"));
        baseBean.writeLog(">>>>>receiptOaOption.jsp>>>>>type="+type);
        String returnDataStr = getRequestHandle(requestid, type);
        baseBean.writeLog(">>>>>receiptOaOption.jsp>>>>>returnDataStr="+returnDataStr);
        JSONObject returnData = JSONObject.parseObject(returnDataStr);
        String code = Util.null2String(returnData.getString("code"));
        String data = Util.null2String(returnData.getString("data"));
        jsonData.put("code", code);
        jsonData.put("data", data);
    } catch (Exception e) {
        baseBean.writeLog(">>>>>receiptOaOption.jsp>>>>>接口请求异常,请查看日志");
        e.printStackTrace();
    }
    out.print(jsonData.toJSONString());
%>

<%!
    /**
     * 获取节点名称
     */
    public static String getNodebaseNodename(String nodeid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select nodename from workflow_nodebase where id=?", nodeid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取节点名称
     */
    public static String getFreenodeNodename(String workflowid, String requestid, String nodeid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select nodename from workflow_freenode where workflowid=? and id=? and requestid=?", workflowid, nodeid, requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取节点名称
     */
    public static String getFormid(String workflowid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select abs(formid) from workflow_base where id=?", workflowid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取QMS类型
     */
    public static int getQmsType(String workflowid, String requesitd) {
        String formid = getFormid(workflowid);
        int type = -1;
        switch (formid) {
            case "3597":
                type = 1;
                break;//MRB
            case "3598":
                type = 2;
                break;//OCAP
            case "3600":
                type = 3;
                break;//内部异常跟踪单
            case "3596":
                if (getDlx(requesitd,"formtable_main_3596","formType") == 0) {
                    type = 4;//内部-CAR
                } else {
                    type = 8;//客诉-CAR
                }
                break;
            case "3595":
                if (getDlx(requesitd,"formtable_main_3595","dlx") == 0) {
                    type = 5;//内部-8D
                } else {
                    type = 9;//客诉-8D
                }
                break;
            case "3863":
                type = 6;
                break;//SCAR-CAR
            case "3864":
                type = 7;
                break;//SCAR-8D
            default:
        }
        return type;


    }

    /**
     * 获取节点名称
     */
    public static int getDlx(String requestid,String tablename,String lx) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select "+lx+" from "+tablename+" where requestid=?", requestid);
        rs.next();
        return Util.getIntValue(rs.getString(1));
    }

    /**
     * 类型转换
     */
    public static int convertType(String logtype) {
        int type = -1;
        switch (logtype) {
            case "h":
                type = 2;
                break;//2、转办
            case "a":
                type = 4;
                break;// 4、意见征询
            case "b":
                type = 5;
                break;// 5、意见征询回复
            case "z":
                type = 6;
                break;// 6、意见征询转办
            default:
        }
        return type;
    }

    /**
     * 获取调用接口处理
     */
    public static String getRequestHandle(String requestid, String logtype) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String returnRespone = "";
        baseBean.writeLog(">>>>>getRequestHandle>>>>requestid=" + requestid + ",logtype=" + logtype);
        rs.executeQuery("select * from (select logid,workflowid,nodeid,remark,operator,destnodeid,receivedpersonids,logtype " +
                "from workflow_requestlog where requestid=? and logtype=? order by logid desc) where rownum=1", requestid, logtype);
        if (rs.next()) {
            try {
                JSONObject jsonData = new JSONObject();
                ResourceComInfo comInfo = new ResourceComInfo();
                //QMS某个模块
                String workflowid = Util.null2String(rs.getString("workflowid"));
                baseBean.writeLog(">>>>>getRequestHandle>>>>workflowid=" + workflowid);
                int qmsType = getQmsType(workflowid, requestid);
                jsonData.put("qmsType", qmsType);
                //当前流程处理人
                String operator = Util.null2String(rs.getString("operator"));
                baseBean.writeLog(">>>>>getRequestHandle>>>>operator=" + operator);
                String wfUser = comInfo.getLastname(operator);
                jsonData.put("wfUser", wfUser);
                //当前流程处理意见
                String remark = Util.null2String(rs.getString("remark"));
                baseBean.writeLog(">>>>>getRequestHandle>>>>remark=" + remark);
                jsonData.put("wfOpinion", remark);
                //流程ID
                String wfId = Util.null2String(requestid);
                jsonData.put("wfId", wfId);
                //下一流程节点处理人
                String receivedpersonids = Util.null2String(rs.getString("receivedpersonids"));
                String[] personids = receivedpersonids.split(",");
                String nextWfUser = "";
                for (String personid : personids) {
                    String username = comInfo.getLastname(personid);
                    nextWfUser += "".equals(nextWfUser) ? username : "," + username;
                }
                jsonData.put("nextWfUser", nextWfUser);
                //当前流程节点名称
                String nodeid = Util.null2String(rs.getString("nodeid"));
                baseBean.writeLog(">>>>>getRequestHandle>>>>nodeid=" + nodeid);
                if (Util.getIntValue(nodeid) < 0) { //当前流程节点是自由节点
                    String wfState = getFreenodeNodename(workflowid, requestid, nodeid);
                    String nextWfState = "";
                    jsonData.put("wfState", wfState);
                    //下一流程节点名称
                    int destnodeid = Util.getIntValue(rs.getString("destnodeid"));
                    baseBean.writeLog(">>>>>getRequestHandle>>>>destnodeid=" + destnodeid);
                    if ((destnodeid > 0 && "3".equals(logtype)) || (destnodeid < 0 && "3".equals(logtype))) {//退回
                        baseBean.writeLog(">>>>>加签退回>>>>destnodeid=" + destnodeid+",logtype="+logtype);
                        nextWfState = destnodeid > 0?getNodebaseNodename(destnodeid + ""):getFreenodeNodename(workflowid, requestid, destnodeid + "");
                        jsonData.put("wfOperate", 1);
                        jsonData.put("nextWfState", nextWfState);
                    }
                    if (destnodeid >= 0 && ("a".equals(logtype) || "h".equals(logtype) || "z".equals(logtype))) {//转办+意见征询
                        baseBean.writeLog(">>>>>转办/意见征询>>>>destnodeid=" + destnodeid+",logtype="+logtype);
                        nextWfState = wfState;
                        jsonData.put("wfOperate", convertType(logtype));
                        jsonData.put("nextWfState", nextWfState);
                    }
                    if (destnodeid < 0 && ("0".equals(logtype) || "b".equals(logtype))) {//流转设定
                        baseBean.writeLog(">>>>>加签>>>>destnodeid=" + destnodeid+",logtype="+logtype);
                        jsonData.put("wfOperate", "0".equals(logtype) ? 3 : 5);
                        nextWfState = getFreenodeNodename(workflowid, requestid, destnodeid + "");
                        jsonData.put("nextWfState", nextWfState);
                    }
                    baseBean.writeLog(">>>>>getRequestHandle>>>>nextWfState=" + nextWfState);
                    if (!"".equals(nextWfState)) {
                        baseBean.writeLog(">>>>>getRequestHandle>>>>jsonData=" + jsonData.toJSONString());
                        returnRespone = receiptOaOption(jsonData.toJSONString());
                        baseBean.writeLog(">>>>>getRequestHandle>>>>returnRespone=" + returnRespone);
                    }
                } else {//当前流程节点是普通节点
                    String wfState = getNodebaseNodename(nodeid);
                    jsonData.put("wfState", wfState);
                    //下一流程节点名称
                    int destnodeid = Util.getIntValue(rs.getString("destnodeid"));
                    baseBean.writeLog(">>>>>getRequestHandle>>>>destnodeid=" + destnodeid);
                    String nextWfState = "";
                    if (destnodeid > 0 && "3".equals(logtype)) {//退回
                        nextWfState = getNodebaseNodename(destnodeid + "");
                        jsonData.put("nextWfState", nextWfState);
                        jsonData.put("wfOperate", 1);
                    }
                    if (destnodeid >= 0 && ("a".equals(logtype) || "h".equals(logtype) || "b".equals(logtype) || "z".equals(logtype))) {//转办+意见征询
                        nextWfState = wfState;
                        jsonData.put("wfOperate", convertType(logtype));
                        jsonData.put("nextWfState", nextWfState);
                    }
                    if (destnodeid < 0 && "0".equals(logtype)) {//流转设定
                        jsonData.put("wfOperate", 3);
                        nextWfState = getFreenodeNodename(workflowid, requestid, destnodeid + "");
                        jsonData.put("nextWfState", nextWfState);
                    }
                    if (!"".equals(nextWfState)) {
                        baseBean.writeLog(">>>>>getRequestHandle>>>>jsonData=" + jsonData.toJSONString());
                        returnRespone = receiptOaOption(jsonData.toJSONString());
                        baseBean.writeLog(">>>>>getRequestHandle>>>>returnRespone=" + returnRespone);
                    }
                }
            } catch (Exception e) {
                baseBean.writeLog(">>>>>getRequestHandle>>>>调用接口请求异常，请查看日志");
                e.printStackTrace();
            }

        }
        return returnRespone;
    }

    /**
     * 获取token
     */
    public static String getToken() {
        String access_token = "";
        JSONObject paramJson = new JSONObject();
        paramJson.put("username", "extadmin");
        paramJson.put("password", "cW1zMTIz");
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, paramJson.toJSONString());
            String url = "https://qms.zhlxsemicon.com/prod-api/auth/SSOLogin";
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Accept", "*/*")
                    .build();
            Response response = client.newCall(request).execute();
            String result = response.body().string();
            if (!"".equals(result)) {
                JSONObject resultJson = JSONObject.parseObject(result);
                String returnCode = resultJson.getString("code");
                String returnData = resultJson.getString("data");
                if ("200".equals(returnCode)) {
                    JSONObject dataJson = JSONObject.parseObject(returnData);
                    access_token = dataJson.getString("access_token");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return access_token;
    }

    /**
     * 调用业务逻辑接口
     */
    public static String receiptOaOption(String paramJson) {
        String result = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, paramJson);
            String token = getToken();
            String url = "https://qms.zhlxsemicon.com/prod-api/qms-zh-jzh/tpi/qef/oa/receiptOaOption";
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Accept", "*/*")
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Authorization", token)
                    .build();
            Response response = client.newCall(request).execute();
            result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
%>




